package com.icss.ah.base.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ResourceBundle;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;

public class HttpFileUtil {
	public static void main(String[] args) {
		try{
			
			String uploadUrl = ResourceBundle.getBundle("icss").getString("uploadUrl");
//		String uploadUrl = "http://192.168.88.53/wxImage/upload/uploadFile.do";
//		String uploadUrl = "http://www.jiefangyx.com/wxImage/upload/uploadFile.do";
			File file = new File("E:\\1.dat");
			
			String fileName = file.getName();
			String fileExt = fileName.substring(fileName.lastIndexOf("."), fileName.length());
			System.out.println("fileExt:"+fileExt);
			
//		System.out.println(HttpFileUtil.uploadFileHttp(file, uploadUrl, "B2B", ".jpg"));
			try {
				FileInputStream fis = new FileInputStream(file);
				System.out.println(HttpFileUtil.uploadFileHttp(fis, uploadUrl, "B2B", ".jpg"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 文件上传-http方式
	 */
	public static String uploadFileHttp(File file, String remoteUrl, String fileProject, String fileExt) {
		if (fileProject != null && !"".equals(fileProject)) {
			if (remoteUrl.indexOf("?") > -1) {
				remoteUrl += "&fileProject="+fileProject;
			} else {
				remoteUrl += "?fileProject="+fileProject;
			}
		}
		if (fileExt != null && !"".equals(fileExt)) {
			if (remoteUrl.indexOf("?") > -1) {
				remoteUrl += "&fileExt="+fileExt;
			} else {
				remoteUrl += "?fileExt="+fileExt;
			}
		}
		return uploadFileHttp(file, remoteUrl);
	}
	/**
	 * 文件上传-http方式
	 */
	public static String uploadFileHttp(File file, String remoteUrl, String fileExt) {
		if (fileExt != null && !"".equals(fileExt)) {
			if (remoteUrl.indexOf("?") > -1) {
				remoteUrl += "&fileExt="+fileExt;
			} else {
				remoteUrl += "?fileExt="+fileExt;
			}
		}
		return uploadFileHttp(file, remoteUrl);
	}
	/**
	 * 文件上传-http方式
	 */
	public static String uploadFileHttp(InputStream is, String remoteUrl, String fileProject, String fileExt) {
		if (fileProject != null && !"".equals(fileProject)) {
			if (remoteUrl.indexOf("?") > -1) {
				remoteUrl += "&fileProject="+fileProject;
			} else {
				remoteUrl += "?fileProject="+fileProject;
			}
		}
		if (fileExt != null && !"".equals(fileExt)) {
			if (remoteUrl.indexOf("?") > -1) {
				remoteUrl += "&fileExt="+fileExt;
			} else {
				remoteUrl += "?fileExt="+fileExt;
			}
		}
		return uploadFileHttp(is, remoteUrl);
	}
	/**
	 * 文件上传-http方式
	 */
	public static String uploadFileHttp(InputStream is, String remoteUrl, String fileExt) {
		if (fileExt != null && !"".equals(fileExt)) {
			if (remoteUrl.indexOf("?") > -1) {
				remoteUrl += "&fileExt="+fileExt;
			} else {
				remoteUrl += "?fileExt="+fileExt;
			}
		}
		return uploadFileHttp(is, remoteUrl);
	}
	/**
	 * 文件上传-http方式
	 */
	private static String uploadFileHttp(File file, String remoteUrl) {
		String result = "";
		if (file != null) {
			try {
				//提交文件
				Part[] parts = { new FilePart("file1", file) };
				PostMethod filePost = new PostMethod(remoteUrl);
				filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
				HttpClient client = new HttpClient();
				client.getHttpConnectionManager().getParams().setConnectionTimeout(2000);
				int status = client.executeMethod(filePost);
				if (status != HttpStatus.SC_OK) {
					System.out.println(status + "--------------fail----");
				} else if (status == HttpStatus.SC_OK) {
					InputStream inputStream = filePost.getResponseBodyAsStream();
					InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
					BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
					StringBuffer buffer = new StringBuffer();
					String str = null;
					while ((str = bufferedReader.readLine()) != null) {
						buffer.append(str);
					}
					bufferedReader.close();
					inputStreamReader.close();
					// 释放资源
					inputStream.close();
					inputStream = null;
					result = buffer.toString();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}
	/**
	 * 文件上传-http方式
	 */
	private static String uploadFileHttp(InputStream is, String remoteUrl) {
		String result = "";
		if (is != null) {
			try {
				//提交文件
				ByteArrayOutputStream baos = new ByteArrayOutputStream();  
				int len = 0;  
				byte[] b = new byte[1024];  
				while ((len = is.read(b, 0, b.length)) != -1) {  
					baos.write(b, 0, len);  
				}  
				byte[] byteArray =  baos.toByteArray();  
				Part[] parts = { new FilePart("file1", new ByteArrayPartSource("file1", byteArray)) };
				PostMethod filePost = new PostMethod(remoteUrl);
				filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
				HttpClient client = new HttpClient();
				client.getHttpConnectionManager().getParams().setConnectionTimeout(2000);
				int status = client.executeMethod(filePost);
				if (status != HttpStatus.SC_OK) {
					System.out.println(status + "--------------fail----");
				} else if (status == HttpStatus.SC_OK) {
					InputStream inputStream = filePost.getResponseBodyAsStream();
					InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
					BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
					StringBuffer buffer = new StringBuffer();
					String str = null;
					while ((str = bufferedReader.readLine()) != null) {
						buffer.append(str);
					}
					bufferedReader.close();
					inputStreamReader.close();
					// 释放资源
					inputStream.close();
					inputStream = null;
					result = buffer.toString();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}
}
